package com.blogSystem.article.service;


import com.blogSystem.article.dao.ArticleDao;
import com.blogSystem.article.dao.CommentMapper;
import com.blogSystem.article.dao.CommentThumbsMapper;
import com.blogSystem.article.pojo.Article;
import com.blogSystem.article.pojo.Comment;
import com.blogSystem.article.pojo.Thumbs;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.python.core.AstList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.plugin.com.event.COMEventHandler;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CommentService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private CommentThumbsMapper commentThumbsMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;



    public void addComment(Comment comment){
        comment.setId(idWorker.nextId()+"");
        comment.setThumbs(0);
        commentMapper.insert(comment);
    }
    public void deleteComment(String id){

        List<Comment> comments=commentMapper.selectByParentId(id);
        if(!comments.isEmpty()){
            List<String> ids=new ArrayList<String>();
            for (Comment comment:comments){
                ids.add(comment.getId());
            }
            commentMapper.deleteByIds(ids);
            commentThumbsMapper.deleteByIds(ids);
        }
        commentMapper.deleteById(id);
        commentThumbsMapper.deleteById(id);
    }
    public void deleteByParentId(String parent_id){
        commentMapper.deleteByParentId(parent_id);


    }
    public void deleteByArticleId(String article_id){
        commentMapper.deleteByArticleId(article_id);
    }
    public List<Comment> findAllParent(String article_id){
        List<Comment> comments=commentMapper.selectParentByArticleId(article_id);
        return comments;
    }
public List<Map> findComment(String article_id){
    List<Comment> p_comments=commentMapper.selectParentByArticleId(article_id);
    List<Map> list=new ArrayList<Map>();
    Map<String ,Object> map = new HashMap<String ,Object>();
        for(Comment p_comment:p_comments){
            List<Comment> c_comments=commentMapper.selectByParentId(p_comment.getId());
            map.put("p_comment",p_comment);
            map.put("c_comments",c_comments);
            list.add(map);
        }
        return list;
}

public List findParentComment(String article_id) {

        return commentMapper.selectParentByArticleId(article_id);
}


public void update(Comment comment){
        commentMapper.update(comment);
}

public List<Comment> findChildComment(String parent_id){
        return commentMapper.selectByParentId(parent_id);
}



}


